<?php 
	//session_start();
	include('header.php'); 
	protectPage();

	if(empty($_POST) === FALSE){
		$required_field = array('currentpassword', 'newpassword', 'newpassword_again');
		foreach ($_POST as $key => $value) {
			# code...
			if(empty($value) && in_array($key, $required_field) === true){
				$errs[] =  "Fields marked with an asterisk are required";
				break 1;
			}
		}

		$pword = $cust_data['password_md5'];
		$cpword = $_POST['currentpassword'];
		//echo $pword;

		if(md5($cpword) === $pword){
			if(trim($_POST['newpassword']) !== trim( $_POST['newpassword_again'])){
				$errs = "Your new Password does not match!";
			}			
			else if(strlen($_POST['newpassword']) < 6){
				$errs = "Your Password must be at least 6 characters!";
			}
			//echo "Fine!";
		}else{
			$errs = "Your Current Password is incorrect!";
		}
	}

	//print_r($errs);
?>


<div class="container"> 
	<h1>Change Password</h1>	

     <?php
     	if(isset($_GET['success']) && empty($_GET['success'])){
     		echo "Your Password has been Changed!";
     	}else{
					if((empty($_POST) === FALSE) && (empty($errs) === TRUE)){
						change_password($session_customer_id, $_POST['newpassword']); 
						header("Location: changepassword.php?success");
					}
					else if(empty($errs) === FALSE){
						print_r($errs);
					}

				?> 
				<form class="form-horizontal" method="post" action="">
					<div class="control-group">
						<label class="control-label" for="currentpassword">Current Password* </label>
						<div class="controls">
							<input type="password" id="currentpassword" name="currentpassword" placeholder="Current Password">
						</div>
					</div>
					<div class="control-group">
						<label class="control-label" for="newpassword">New Password* </label>
						<div class="controls">
							<input type="password" id="newpassword" name="newpassword" placeholder="New Password">
						</div>
					</div>
					<div class="control-group">
						<label class="control-label" for="newpassword_again">New Password* </label>
						<div class="controls">
							<input type="password" id="newpassword_again" name="newpassword_again" placeholder="New Password">
						</div>		
					<div class="control-group">
						<p><em>(Passwords are case-sensitive and must be at least 6 characters long)</em></p>
			        <div class="controls">
			          <button type="reset" name="reset" class="btn btn-large btn-primary">Reset form</button>&nbsp;&nbsp;
			         <button type="submit" name="register" class="btn btn-large btn-primary">Change Password</button>
			       </div>
			     </div>
			    
				</form>
</div>
<?php 
}
	include('footer.php'); 
	?>